import { menu_list } from '../../template/data';
import { navigateTo } from '../../utils/util';
const app = getApp()
Page({
    data: {
        to_menu_sort_view: '',                          // 菜单分类 锚点
        to_menu_list_view: '',                          // 菜单列表 锚点
        sort_active: '0',                               // 菜单分类激活状态

        sortTop:'0',
        menuTop:'0',

        wishlist_show: false,                            // 购物清单显示 
        sum_total: '',                                  // 商品总数   
        show: false,                                    // 快捷导航是否显示          
        content_height: '',                             // 已选商品列表的高度
        scroll_height: '0',                             // scroll-view高度
        menu_list: [],                                  // 菜单列表渲染数据 (不包括商品数量)         
        sort_num:[],                                    // 菜单分类的显示数量  
        wishlist:[],                                    // 已选商品列表数据 

        data: [],                                       // 商品列表数据 (用于计算各列表间数量同步显示)
        total_price: 0                                  // 商品总价

    },
    onLoad: function () {
        const { wishlist_show } = this.data;

        this.set_scrollView();                          // 菜单列表高度 初始化
        this.init_data(menu_list);                      // 数据初始化
        
        if (wishlist_show) {                            
            this.get_height();
            // this.wishlist(menu_list);
        };
    },
    // =================首页使用到的方法===============

    // ======这是需要被请求的菜单分类以及列表的所有数据
    //                  menu_list
    // ======这是需要被请求的菜单分类以及列表的所有数据

    // 数据初始化
    init_data: function (menu_list){
        // 所有数据
        let array1 = [], wishlist=[];
        let sort_num = [], sum_total = 0, total_price=0;

        menu_list.map((items,index)=>{
            let array2 = [], num = 0;

            items.list.map((item,idx)=>{
                let obj = {id:item.id, name: item.name,price:item.price,init_num:item.init_num};
                if (parseInt(item.init_num)>0){
                    let pl = parseFloat(item.price), pi = parseInt(item.init_num);
                    let sum = parseFloat((pl * pi));

                    let item_price = parseFloat((pl * pi).toFixed(2));

                    let obj_wishlist = { 
                        id: item.id, name: item.name, price: item.price, 
                        init_num: item.init_num,index:index ,idx:idx,
                        item_price: item_price
                    };
                    
                    total_price = parseFloat(parseFloat(total_price + sum).toFixed(2));
                    
                    wishlist.push(obj_wishlist);
                };
                array2.push(obj); 
                // =====总数
                sum_total += parseInt(item.init_num);
                // =====列表数量
                num += parseInt(item.init_num);
            });

            sort_num.push(num); array1.push(array2); 
        });
        this.setData({ 
            data: array1,
            sort_num: sort_num,
            menu_list: menu_list,
            sum_total: sum_total, 
            wishlist: wishlist,
            total_price: total_price
        });
    },
    // 菜单列表数量加减操作
    modify_num:function(e){
        let { data, wishlist, total_price } = this.data;
        const tip = e.target.dataset.tip;
        const idx = e.target.dataset.idx;                 //位置索引 
        const index = e.target.dataset.index;             //分类索引
        let num = parseInt(data[index][idx].init_num);    //当前数量

        if (tip == "add") {  // 点击数量加
            let id = data[index][idx].id;
            let status=false;
            data[index][idx].init_num = parseInt(data[index][idx].init_num)+1;
           
           // 同步已选商品列表数据 wishlist
            wishlist.map((items,idx)=>{  //如果列表中已有该菜品,修改数量
                if (id == items.id){
                    status=true;
                    items.init_num = parseInt(items.init_num)+1;
                    
                    let pl = parseFloat(items.price), pi = parseInt(items.init_num);
                    items.item_price = parseFloat((pl * pi).toFixed(2));

                    let price = parseFloat(items.price);
                    total_price = parseFloat(parseFloat(total_price + price).toFixed(2));
                    
                }
            });

            if (!status) {              //如果列表中没有该菜品,添加该菜品
                let obj = data[index][idx];
                obj.index=index; obj.idx=idx; 
                obj.item_price = parseFloat((parseFloat(obj.price) * parseInt(obj.init_num)).toFixed(2));
                wishlist.unshift(obj);
                
                let price = parseFloat(obj.price);
                total_price = parseFloat(parseFloat(total_price + price).toFixed(2));
            };

            this.setData({
                wishlist: wishlist,
                total_price: total_price
            });
           

        } else if (tip == "minus") {    // 点击数量减
            if(num>=0){
                data[index][idx].init_num = parseInt(data[index][idx].init_num) - 1;
                let id = data[index][idx].id;
                let status = false;
                // 同步已选商品列表数据 wishlist
                wishlist.map((items, idx) => {
                    if (id == items.id) {
                        status = true;
                        if (items.init_num>1){

                            items.init_num = parseInt(items.init_num) - 1;

                        } else if (items.init_num == 1){
                            wishlist.splice(idx,1);
                        }
                        let price = parseFloat(items.price);
                        total_price = parseFloat(parseFloat(total_price - price).toFixed(2));
                        items.item_price = parseFloat(parseFloat(items.item_price - parseFloat(items.price)).toFixed(2));
                    }
                });
                this.setData({
                    wishlist: wishlist,
                    total_price: total_price
                });
            }
        };

        // 同步菜单分类列表以及商品总数数据 sort_num sum_total
        let sort_num = [], sum_total=0;
        data.map((items,idx)=>{
            let num=0
            items.map((item)=>{
                num += parseInt(item.init_num); 
                sum_total += parseInt(item.init_num);
            });
            sort_num.push(num);
        });
        this.setData({ data: data, sort_num: sort_num, sum_total: sum_total });
    },
    // 清空已选商品
    clear_wishlist:function(){
        const that = this;
        wx.showModal({
            title: '提示',
            content: '确认清空已选商品',
            success: function (res) {
                if (res.confirm) {

                    const { data, wishlist, sort_num } = that.data;
                    //处理菜单列表的数据
                    data.map((items, index) => {
                        sort_num[index] = 0;
                        items.map((item, idx) => { item.init_num = 0 })
                    });

                    that.setData({ 
                        data: data, 
                        wishlist: [],
                        sort_num: sort_num,
                        wishlist_show: false,
                        total_price: 0,
                        sum_total:0    
                    });
                    
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        });
    },
    // wishlist 显示与隐藏
    wishlist_close:function(){
        const { wishlist_show } = this.data;
        let status, wishlist=[];
        wishlist_show ? status = false : status = true;
        this.setData({
            wishlist_show: status
        })
    },
    // 已点菜单 ==> 数量加减操作
    wishlist_op: function (e){
        const { wishlist, data, sum_total, sort_num}=this.data;
        let { total_price } = this.data;
        const tip = e.target.dataset.tip;
        const id = e.target.dataset.id;
        const index0 = e.target.dataset.index;
        const idx0 = e.target.dataset.idx;

        if (tip == 'add') {
            wishlist.map((item,idx)=>{
                if (index0 == item.index&&idx0==item.idx){
                    item.init_num = parseInt(item.init_num)+1;
                    sort_num[index0] = parseInt(sort_num[index0]) + 1;
                    data[index0][idx0].init_num = parseInt(data[index0][idx0].init_num) + 1;

                    item.item_price = parseFloat(parseFloat(item.item_price + parseFloat(item.price)).toFixed(2));
                    let price = parseFloat(item.price);
                    total_price = parseFloat(parseFloat(total_price + price).toFixed(2));
                }
            });

            this.setData({ 
                wishlist: wishlist, 
                data: data, 
                sum_total: sum_total + 1, 
                sort_num: sort_num, 
                total_price:total_price
            });

        } else if (tip == 'minus') {
            
            wishlist.map((item, idx) => {
                if (index0 == item.index && idx0 == item.idx) {
                    if (parseInt(item.init_num)>1){
                        item.init_num = parseInt(item.init_num) - 1;
                        sort_num[index0] = parseInt(sort_num[index0]) - 1;
                        data[index0][idx0].init_num = parseInt(data[index0][idx0].init_num) - 1;

                    } else if (parseInt(item.init_num) == 1){
                        wishlist.splice(idx,1);
                        data[index0][idx0].init_num = 0;
                        sort_num[index0] = 0;
                    }
                    item.item_price = parseFloat(parseFloat(item.item_price - parseFloat(item.price)).toFixed(2));
                    let price = parseFloat(item.price);
                    total_price = parseFloat(parseFloat(total_price - price).toFixed(2));
                }
            });
            this.setData({ 
                wishlist: wishlist, 
                data: data, 
                sum_total: sum_total - 1, 
                sort_num: sort_num,
                total_price: total_price,
            });
        }
    
    },
    // 设置scroll-view高度
    set_scrollView: function () {
        const that = this;
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    scroll_height: res.windowHeight
                });
            }
        });
    },
    // 获取相关元素高度 
    get_height: function () {
        const that = this;
        var query = wx.createSelectorQuery();
        query.select('.content').boundingClientRect(function (rect) {
            that.setData({ content_height: rect.height })
        }).exec();
    },
    // 锚点跳转
    set_toView: function (e) {
        const idx = e.target.dataset.idx;
        // sort_active
        let id = 'item_title' + idx;
        this.setData({
            view: id,
            sort_active: idx
        });
    },
    // 快捷导航
    quick_nav: function () {
        const { show } = this.data;
        let status;
        show ? status = false : status = true;
        this.setData({
            show: status
        })
    },
    // 页面跳转
    navigateTo:function(e){
        // 传递商品id 以及数量 
        const tip = e.target.dataset.tip;
        let param ="";
        if (tip =="payment"){
            param = '?order_sn=124648748'; 
        }
         
        navigateTo(tip, param);
    },

})